package org.dream.ui.chart.impl;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import org.dream.adam.DoubleImage;
import org.dream.domain.Daydeal;
import org.dream.ui.chart.Chart;
import org.dream.ui.chart.Plot;

/**
 * 价格K线
 * 
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class ImageKPlot extends Plot {
	private List<Daydeal> daydealList;
	private int maxX;
	private int MinX;
	private int now;
	private List<Daydeal> doubleImageList = new ArrayList<Daydeal>();

	/**
	 * 
	 * @param daydealList
	 * @param chart
	 * @param index
	 * @param now几天前0代表今天
	 *            ，1代表昨天
	 */
	public ImageKPlot(List<Daydeal> daydealList, Chart chart, int index, int now, int num) {
		super(chart, index);
		// now = daydealList.size() - pre;
		this.now = now;
		this.daydealList = daydealList;
		title = daydealList.get(0).getName() + "(日线)";
		// now-1为index，再加一才是最后的是now
		if (now - num-1 >= 0) {
			doubleImageList = DoubleImage.getDaydealsImage(daydealList.subList(now - num-1, now), num);
		} else {
			doubleImageList = DoubleImage.getDaydealsImage(daydealList.subList(0, now), num);
		}

	}

	@Override
	public void beforeDraw() {

		MinX = chart.getXAxis().getXAxisModel().getMinValue() - 1;
		maxX = chart.getXAxis().getXAxisModel().getMaxValue() - 1;

	}

	@Override
	public Double getMaxY() {
		Double maxY = -Double.MAX_VALUE;
		for (int i = 0; i < doubleImageList.size(); i++) {
			Daydeal daydeal = doubleImageList.get(i);
			if (daydeal.getHIGH().compareTo(maxY) > 0) {
				maxY = daydeal.getHIGH();
			}
		}
		return maxY;
	}

	@Override
	public Double getMinY() {
		Double minY = Double.MAX_VALUE;
		for (int i = 0; i < doubleImageList.size(); i++) {
			Daydeal daydeal = doubleImageList.get(i);
			if (daydeal.getLOW().compareTo(minY) < 0) {
				minY = daydeal.getLOW();
			}
		}
		return minY;
	}

	@Override
	public void draw(Graphics g) {
		// now line
		int x = chart.getXAxis().getXAxisModel().translateValueToPx(now)
				+ (int) chart.getXAxis().getXAxisModel().getTickSpacingPixel() / 2;

		g.drawLine(x, chart.getChartModel().getPixelYTop(), x, chart.getChartModel().getPixelYButtom());
		for (int i = 0; i < doubleImageList.size(); i++) {
			Daydeal daydeal = doubleImageList.get(i);
			int dayXPixel = chart.getXCoordinatePixel(new Double(i + now + 1));
			int OPEN = chart.getYCoordinatePixel(daydeal.getOPEN(), index);
			int CLOSE = chart.getYCoordinatePixel(daydeal.getCLOSE(), index);
			int HIGH = chart.getYCoordinatePixel(daydeal.getHIGH(), index);
			int LOW = chart.getYCoordinatePixel(daydeal.getLOW(), index);
			// 58:18
			int halfWidth = (int) (chart.getXAxis().getXAxisModel().getTickSpacingPixel() / (58 + 18) * 58 / 2);
			if (daydeal.getOPEN() < daydeal.getCLOSE()) {
				g.setColor(Color.PINK);
				g.drawRect(dayXPixel - halfWidth, CLOSE, halfWidth * 2, Math.abs(CLOSE - OPEN));
				g.drawLine(dayXPixel, LOW, dayXPixel, OPEN);
				g.drawLine(dayXPixel, CLOSE, dayXPixel, HIGH);
			} else if (daydeal.getOPEN() > daydeal.getCLOSE()) {
				g.setColor(Color.GREEN);
				g.drawRect(dayXPixel - halfWidth, OPEN, halfWidth * 2, Math.abs(OPEN - CLOSE));
				g.fillRect(dayXPixel - halfWidth, OPEN, halfWidth * 2, Math.abs(OPEN - CLOSE));
				g.drawLine(dayXPixel, LOW, dayXPixel, CLOSE);
				g.drawLine(dayXPixel, OPEN, dayXPixel, HIGH);
			} else {
				g.setColor(Color.WHITE);
				g.drawRect(dayXPixel - halfWidth, CLOSE, halfWidth * 2, Math.abs(CLOSE - OPEN));
				g.drawLine(dayXPixel, LOW, dayXPixel, OPEN);
				g.drawLine(dayXPixel, CLOSE, dayXPixel, HIGH);

			}

		}

	}
}
